package com.tg.app.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.appbase.custom.constant.DevcieFunctionConstants;
import com.tg.app.R;
import com.tg.app.helper.DeviceHelper;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.data.bean.DeviceItem;
import com.tg.data.helper.DeviceItemHelper;

import java.util.List;


public class DeviceBusinessAdapter extends RecyclerView.Adapter<DeviceBusinessAdapter.SimpleViewHolder> {
    private List<Integer> list;
    private DeviceAdapter.OnItemClickListener onItemClickListener;
    private DeviceItem deviceItem;

    public void setDeviceItem(DeviceItem deviceItem) {
        this.deviceItem = deviceItem;
    }

    public void setOnItemClickListener(DeviceAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public DeviceBusinessAdapter(List<Integer> items) {
        list = items;
    }

    @NonNull
    @Override
    public SimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_device_business_list_item, parent, false);
        return new SimpleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (holder != null) {
            Integer item = list.get(position);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position, item);
                    }
                }
            });

            if (deviceItem == null) return;
            holder.iconArrow.setImageResource(R.drawable.ic_tange_global_right_arrow_black);
            switch (item) {
                case DevcieFunctionConstants.LOCK_DELL_NO_CALL:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_no_call_list);
                    holder.nameText.setTextColor(ResourcesUtil.getResources().getColor(R.color.lock_dell_no_call_color));
                    if (DeviceHelper.isLamp(deviceItem)){
                        holder.nameText.setText(R.string.device_function_lamp_no_call);
                    }else{
                        holder.nameText.setText(R.string.device_function_lock_dell_no_call);
                    }

                    break;
                case DevcieFunctionConstants.LOCK_DELL_LOG:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_lock_dell_log);
                    holder.nameText.setText(R.string.device_function_lock_dell_log);
                    break;

                case DevcieFunctionConstants.LOCK_MESSAGE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_msg_list);
                    holder.nameText.setText(R.string.device_function_lock_dell_msg);
                    break;
                case DevcieFunctionConstants.MESSAGE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_msg_list);
                    holder.nameText.setText(R.string.device_function_msg);

                    break;
                case DevcieFunctionConstants.SERVICE_4G:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_4g_vip_list);
                    holder.nameText.setText(R.string.device_function_4g);

                    break;
                case DevcieFunctionConstants.SERVICE_AI:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_ai_service_list_new);
                    if (deviceItem.server_bought != null && deviceItem.server_bought.ai_server == 0) {
                        holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_ai_service_list_red);
                    } else {
                        holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_ai_service_list_new);
                    }
                    if (DeviceItemHelper.isSuperAiService(deviceItem)){
                        holder.nameText.setText(R.string.device_function_super_ai);
                    }else{
                        holder.nameText.setText(R.string.device_function_ai);
                    }

                    break;
                case DevcieFunctionConstants.SERVICE_CAR:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_car_vip_list);
                    holder.nameText.setText(R.string.settings_device_car);

                    break;
                case DevcieFunctionConstants.SERVICE_CLOUD:
                    if (DeviceHelper.isDoorBell(deviceItem) || DeviceHelper.isLockBell(deviceItem) || DeviceHelper.isBirdFeeder(deviceItem)){
                        holder.nameText.setText(R.string.txt_vip);
                        holder.iconImage.setImageResource(R.drawable.ic_tange_global_vip);
                    }else{
                        if (deviceItem.server_bought != null && deviceItem.server_bought.server == 0) {
                            holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_cloud_video_free_list);
                        } else {
                            holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_cloud_video_list);
                        }
                        holder.nameText.setText(R.string.text_label_cloud_video);
                    }

                    break;
                case DevcieFunctionConstants.CAR_TRACK:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_drive_record_list);
                    holder.nameText.setText(R.string.device_function_track);
                    break;
                case DevcieFunctionConstants.CARD_VIDEO:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_live_video);
                    holder.nameText.setText(R.string.text_label_sd_card_video);
                    break;

                case DevcieFunctionConstants.DEVICE_HOMEWORK_PHOTO:
                    holder.iconImage.setImageResource(R.drawable.ic_global_study_assistant_service);
                    holder.nameText.setText(R.string.settings_homework_photo);
                    break;
                case DevcieFunctionConstants.LAMP_STUDY_SERVICE:
                    holder.iconImage.setImageResource(R.drawable.ic_global_study_assistant_service);
                    holder.nameText.setText(R.string.txt_study_assistant_service);
                    break;
                case DevcieFunctionConstants.SHARE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_share_list);
                    holder.nameText.setText(R.string.device_function_share);
                    break;
                case DevcieFunctionConstants.SETTINGS:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_setting_list);
                    holder.nameText.setText(R.string.device_function_settings);
                    break;

                case DevcieFunctionConstants.CAR_ELECTRIC_FENCE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_global_icon_electric_fence_list);
                    holder.nameText.setText(R.string.device_function_electric_fence);
                    break;
                case DevcieFunctionConstants.PIC_BIRD_SERVICE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_pic_bird);
                    holder.nameText.setText(R.string.pictures_birds);
                    break;
                case DevcieFunctionConstants.STATISTICS_SERVICE:
                    holder.iconImage.setImageResource(R.drawable.ic_tange_statistics);
                    holder.nameText.setText(R.string.statistics_birds);
                    break;
            }

        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class SimpleViewHolder extends RecyclerView.ViewHolder {
        ImageView iconImage;
        TextView nameText;
        ImageView iconArrow;

        public SimpleViewHolder(@NonNull View itemView) {
            super(itemView);
            iconImage = itemView.findViewById(R.id.iv_device_business_list_item_icon);
            nameText = itemView.findViewById(R.id.tv_layout_device_business_list_item_name);
            iconArrow = itemView.findViewById(R.id.iv_device_business_list_item_icon_right);
        }
    }

}
